package net.xiaoyu233.mitemod.miteite.tileentity.Recipes;

import net.minecraft.*;
import java.util.HashMap;
import java.util.Map;

public class RecyclingRecipes {

    private static final Map<Item, Item> materialToIngotMap = new HashMap<>();
    private static final Map<Item, Item> materialToNuggetMap = new HashMap<>(); // 新增：材料到粒的映射
    private static final Map<Item, Integer> baseIngotCountMap = new HashMap<>();
    private static final Map<Item, Integer> materialLevelMap = new HashMap<>(); // 材料所需的最低回收台等级

    static {
        // 初始化工具/装备到锭和粒的映射
        initRecipes();
    }

    private static void initRecipes() {
        // 铜制品 (等级 0)
        addRecipe(Item.pickaxeCopper, Item.ingotCopper, Item.copperNugget, 3, 0);//稿子
        addRecipe(Item.axeCopper, Item.ingotCopper, Item.copperNugget, 3, 0);//斧子
        addRecipe(Item.shovelCopper, Item.ingotCopper, Item.copperNugget, 1, 0);//铲子
        addRecipe(Item.hoeCopper, Item.ingotCopper, Item.copperNugget, 2, 0);//锄头
        addRecipe(Item.swordCopper, Item.ingotCopper, Item.copperNugget, 2, 0);//剑
        addRecipe(Item.helmetCopper, Item.ingotCopper, Item.copperNugget, 5, 0);//铜头盔
        addRecipe(Item.plateCopper, Item.ingotCopper, Item.copperNugget, 8, 0);//胸甲
        addRecipe(Item.legsCopper, Item.ingotCopper, Item.copperNugget, 7, 0);//裤子
        addRecipe(Item.bootsCopper, Item.ingotCopper, Item.copperNugget, 4, 0);//鞋子
        addRecipe(Item.battleAxeCopper, Item.ingotCopper, Item.copperNugget, 4, 0);//战斧
        addRecipe(Item.mattockCopper, Item.ingotCopper, Item.copperNugget, 4, 0);//鹤嘴锄
        addRecipe(Item.scytheCopper, Item.ingotCopper, Item.copperNugget, 2, 0);//镰刀
        addRecipe(Item.shearsCopper, Item.ingotCopper, Item.copperNugget, 2, 0);//剪刀
        addRecipe(Item.daggerCopper, Item.ingotCopper, Item.copperNugget, 1, 0);//短剑
        addRecipe(Item.warHammerCopper, Item.ingotCopper, Item.copperNugget, 5, 0);//战锤
        addRecipe(Item.hatchetSilver, Item.ingotSilver, Item.silverNugget, 2, 1);//手斧头

        // 银制品 (等级 1)
        addRecipe(Item.pickaxeSilver, Item.ingotSilver, Item.silverNugget, 3, 1);
        addRecipe(Item.axeSilver, Item.ingotSilver, Item.silverNugget, 3, 1);
        addRecipe(Item.shovelSilver, Item.ingotSilver, Item.silverNugget, 1, 1);
        addRecipe(Item.hoeSilver, Item.ingotSilver, Item.silverNugget, 2, 1);
        addRecipe(Item.swordSilver, Item.ingotSilver, Item.silverNugget, 2, 1);
        addRecipe(Item.helmetSilver, Item.ingotSilver, Item.silverNugget, 5, 1);
        addRecipe(Item.plateSilver, Item.ingotSilver, Item.silverNugget, 8, 1);
        addRecipe(Item.legsSilver, Item.ingotSilver, Item.silverNugget, 7, 1);
        addRecipe(Item.bootsSilver, Item.ingotSilver, Item.silverNugget, 4, 1);
        addRecipe(Item.battleAxeSilver, Item.ingotSilver, Item.silverNugget, 4, 1);
        addRecipe(Item.mattockSilver, Item.ingotSilver, Item.silverNugget, 4, 1);
        addRecipe(Item.scytheSilver, Item.ingotSilver, Item.silverNugget, 2, 1);
        addRecipe(Item.shearsSilver, Item.ingotSilver, Item.silverNugget, 2, 1);
        addRecipe(Item.daggerSilver, Item.ingotSilver, Item.silverNugget, 1, 1);
        addRecipe(Item.warHammerSilver, Item.ingotSilver, Item.silverNugget, 5, 1);

        // 金制品 (等级 2)
        addRecipe(Item.pickaxeGold, Item.ingotGold, Item.goldNugget, 3, 2);
        addRecipe(Item.axeGold, Item.ingotGold, Item.goldNugget, 3, 2);
        addRecipe(Item.shovelGold, Item.ingotGold, Item.goldNugget, 1, 2);
        addRecipe(Item.hoeGold, Item.ingotGold, Item.goldNugget, 2, 2);
        addRecipe(Item.swordGold, Item.ingotGold, Item.goldNugget, 2, 2);
        addRecipe(Item.helmetGold, Item.ingotGold, Item.goldNugget, 5, 2);
        addRecipe(Item.plateGold, Item.ingotGold, Item.goldNugget, 8, 2);
        addRecipe(Item.legsGold, Item.ingotGold, Item.goldNugget, 7, 2);
        addRecipe(Item.bootsGold, Item.ingotGold, Item.goldNugget, 4, 2);
        addRecipe(Item.battleAxeGold, Item.ingotGold, Item.goldNugget, 4, 2);
        addRecipe(Item.mattockGold, Item.ingotGold, Item.goldNugget, 4, 2);
        addRecipe(Item.scytheGold, Item.ingotGold, Item.goldNugget, 2, 2);
        addRecipe(Item.shearsGold, Item.ingotGold, Item.goldNugget, 2, 2);
        addRecipe(Item.daggerGold, Item.ingotGold, Item.goldNugget, 1, 2);
        addRecipe(Item.warHammerGold, Item.ingotGold, Item.goldNugget, 5, 2);

        // 铁制品 (等级 3)
        addRecipe(Item.pickaxeIron, Item.ingotIron, Item.ironNugget, 3, 3);
        addRecipe(Item.axeIron, Item.ingotIron, Item.ironNugget, 3, 3);
        addRecipe(Item.shovelIron, Item.ingotIron, Item.ironNugget, 1, 3);
        addRecipe(Item.hoeIron, Item.ingotIron, Item.ironNugget, 2, 3);
        addRecipe(Item.swordIron, Item.ingotIron, Item.ironNugget, 2, 3);
        addRecipe(Item.helmetIron, Item.ingotIron, Item.ironNugget, 5, 3);
        addRecipe(Item.plateIron, Item.ingotIron, Item.ironNugget, 8, 3);
        addRecipe(Item.legsIron, Item.ingotIron, Item.ironNugget, 7, 3);
        addRecipe(Item.bootsIron, Item.ingotIron, Item.ironNugget, 4, 3);
        addRecipe(Item.battleAxeIron, Item.ingotIron, Item.ironNugget, 4, 3);
        addRecipe(Item.mattockIron, Item.ingotIron, Item.ironNugget, 4, 3);
        addRecipe(Item.scytheIron, Item.ingotIron, Item.ironNugget, 2, 3);
        addRecipe(Item.daggerIron, Item.ingotIron, Item.ironNugget, 1, 3);
        addRecipe(Item.warHammerIron, Item.ingotIron, Item.ironNugget, 5, 3);

        // 远古金属制品 (等级 5)
        addRecipe(Item.pickaxeAncientMetal, Item.ingotAncientMetal, Item.ancientMetalNugget, 3, 5);
        addRecipe(Item.axeAncientMetal, Item.ingotAncientMetal, Item.ancientMetalNugget, 3, 5);
        addRecipe(Item.shovelAncientMetal, Item.ingotAncientMetal, Item.ancientMetalNugget, 1, 5);
        addRecipe(Item.hoeAncientMetal, Item.ingotAncientMetal, Item.ancientMetalNugget, 2, 5);
        addRecipe(Item.swordAncientMetal, Item.ingotAncientMetal, Item.ancientMetalNugget, 2, 5);
        addRecipe(Item.helmetAncientMetal, Item.ingotAncientMetal, Item.ancientMetalNugget, 5, 5);
        addRecipe(Item.plateAncientMetal, Item.ingotAncientMetal, Item.ancientMetalNugget, 8, 5);
        addRecipe(Item.legsAncientMetal, Item.ingotAncientMetal, Item.ancientMetalNugget, 7, 5);
        addRecipe(Item.bootsAncientMetal, Item.ingotAncientMetal, Item.ancientMetalNugget, 4, 5);
        addRecipe(Item.battleAxeAncientMetal, Item.ingotAncientMetal, Item.ancientMetalNugget, 4, 5);
        addRecipe(Item.mattockAncientMetal, Item.ingotAncientMetal, Item.ancientMetalNugget, 4, 5);
        addRecipe(Item.scytheAncientMetal, Item.ingotAncientMetal, Item.ancientMetalNugget, 2, 5);
        addRecipe(Item.shearsAncientMetal, Item.ingotAncientMetal, Item.ancientMetalNugget, 2, 5);
        addRecipe(Item.daggerAncientMetal, Item.ingotAncientMetal, Item.ancientMetalNugget, 1, 5);
        addRecipe(Item.warHammerAncientMetal, Item.ingotAncientMetal, Item.ancientMetalNugget, 5, 5);

        // 秘银制品 (等级 4)
        addRecipe(Item.pickaxeMithril, Item.ingotMithril, Item.mithrilNugget, 3, 4);
        addRecipe(Item.axeMithril, Item.ingotMithril, Item.mithrilNugget, 3, 4);
        addRecipe(Item.shovelMithril, Item.ingotMithril, Item.mithrilNugget, 1, 4);
        addRecipe(Item.hoeMithril, Item.ingotMithril, Item.mithrilNugget, 2, 4);
        addRecipe(Item.swordMithril, Item.ingotMithril, Item.mithrilNugget, 2, 4);
        addRecipe(Item.helmetMithril, Item.ingotMithril, Item.mithrilNugget, 5, 4);
        addRecipe(Item.plateMithril, Item.ingotMithril, Item.mithrilNugget, 8, 4);
        addRecipe(Item.legsMithril, Item.ingotMithril, Item.mithrilNugget, 7, 4);
        addRecipe(Item.bootsMithril, Item.ingotMithril, Item.mithrilNugget, 4, 4);
        addRecipe(Item.battleAxeMithril, Item.ingotMithril, Item.mithrilNugget, 4, 4);
        addRecipe(Item.mattockMithril, Item.ingotMithril, Item.mithrilNugget, 4, 4);
        addRecipe(Item.scytheMithril, Item.ingotMithril, Item.mithrilNugget, 2, 4);
        addRecipe(Item.shearsMithril, Item.ingotMithril, Item.mithrilNugget, 2, 4);
        addRecipe(Item.daggerMithril, Item.ingotMithril, Item.mithrilNugget, 1, 4);
        addRecipe(Item.warHammerMithril, Item.ingotMithril, Item.mithrilNugget, 5, 4);

        // 艾德曼制品 (等级 6)
        addRecipe(Item.pickaxeAdamantium, Item.ingotAdamantium, Item.adamantiumNugget, 3, 6);
        addRecipe(Item.axeAdamantium, Item.ingotAdamantium, Item.adamantiumNugget, 3, 6);
        addRecipe(Item.shovelAdamantium, Item.ingotAdamantium, Item.adamantiumNugget, 1, 6);
        addRecipe(Item.hoeAdamantium, Item.ingotAdamantium, Item.adamantiumNugget, 2, 6);
        addRecipe(Item.swordAdamantium, Item.ingotAdamantium, Item.adamantiumNugget, 2, 6);
        addRecipe(Item.helmetAdamantium, Item.ingotAdamantium, Item.adamantiumNugget, 5, 6);
        addRecipe(Item.plateAdamantium, Item.ingotAdamantium, Item.adamantiumNugget, 8, 6);
        addRecipe(Item.legsAdamantium, Item.ingotAdamantium, Item.adamantiumNugget, 7, 6);
        addRecipe(Item.bootsAdamantium, Item.ingotAdamantium, Item.adamantiumNugget, 4, 6);
        addRecipe(Item.battleAxeAdamantium, Item.ingotAdamantium, Item.adamantiumNugget, 4, 6);
        addRecipe(Item.mattockAdamantium, Item.ingotAdamantium, Item.adamantiumNugget, 4, 6);
        addRecipe(Item.scytheAdamantium, Item.ingotAdamantium, Item.adamantiumNugget, 2, 6);
        addRecipe(Item.shearsAdamantium, Item.ingotAdamantium, Item.adamantiumNugget, 2, 6);
        addRecipe(Item.daggerAdamantium, Item.ingotAdamantium, Item.adamantiumNugget, 1, 6);
        addRecipe(Item.warHammerAdamantium, Item.ingotAdamantium, Item.adamantiumNugget, 5, 6);

        // 振金制品 (等级 7)
        // 假设有振金工具和装备，这里使用VIBRANIUM_INGOT作为示例
        // addRecipe(Items.pickaxeVibranium, Items.VIBRANIUM_INGOT, Items.VIBRANIUM_NUGGET, 3, 7);
        // addRecipe(Items.axeVibranium, Items.VIBRANIUM_INGOT, Items.VIBRANIUM_NUGGET, 3, 7);
        // 等等...
    }

    private static void addRecipe(Item tool, Item ingot, Item nugget, int baseCount, int level) {
        materialToIngotMap.put(tool, ingot);
        materialToNuggetMap.put(tool, nugget);
        baseIngotCountMap.put(tool, baseCount);
        materialLevelMap.put(tool, level);
    }

    // 检查物品是否可回收，并检查回收台等级是否足够
    public static boolean canRecycle(ItemStack stack, int tableLevel) {
        if (stack == null) return false;

        Item item = stack.getItem();
        // 检查是否是工具或装备并且有对应的配方，以及回收台等级是否足够
        return (item instanceof ItemTool || item instanceof ItemArmor || item instanceof ItemSword) &&
                materialToIngotMap.containsKey(item) &&
                materialLevelMap.get(item) <= tableLevel;
    }

    // 获取对应的锭
    public static Item getIngotForItem(Item item) {
        return materialToIngotMap.get(item);
    }

    // 获取对应的粒
    public static Item getNuggetForItem(Item item) {
        return materialToNuggetMap.get(item);
    }

    // 获取基础锭数量
    public static int getBaseIngotCount(Item item) {
        return baseIngotCountMap.getOrDefault(item, 1);
    }

    // 获取材料所需的最低回收台等级
    public static int getMaterialLevel(Item item) {
        return materialLevelMap.getOrDefault(item, 0);
    }

    // 获取所有可回收物品
    public static Item[] getRecyclableItems() {
        return materialToIngotMap.keySet().toArray(new Item[0]);
    }

    // 添加自定义配方（供其他mod或扩展使用）
    public static void addCustomRecipe(Item tool, Item ingot, Item nugget, int baseCount, int level) {
        addRecipe(tool, ingot, nugget, baseCount, level);
    }

    public static String getTableLevelName(int tableLevel) {
        switch (tableLevel) {
            case 0: return "铜";
            case 1: return "银";
            case 2: return "金";
            case 3: return "铁";
            case 4: return "秘银";
            case 5: return "远古金属";
            case 6: return "艾德曼";
            case 7: return "振金";
            default: return "未知";
        }
    }
}